﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10

int a[MAX][MAX];

int isOut(int i, int j)//是否越界
{
	if (i < 0 || i>9 || j < 0 || j>9)
		return 0;//out
	return 1;//in
}

void print()//应用于打印棋盘
{
	int i, j;
	printf("p1玩家%c\n", 1);
	printf("p2玩家%c\n", 2);
	printf("  ");
	for (i = 0;i < 10;i++)
		printf("%d ", i);
	printf("\n");
	for (i = 0;i < 10;i++)
	{
		printf("%d ", i);
		for (j = 0;j < 10;j++)
			if (a[i][j] == 0)
				printf(". ");
			else if (a[i][j] == 1)
				printf("%c ", 1);
			else
				printf("%c ", 2);
		printf("\n");
	}
}

void testRight(int i, int j, int flag)
{
	int m, n, sum, row, col;
	for (m = i - 4, n = j - 4;m <= i, n <= j;m++, n++)
	{
		sum = 0;
		if (isOut(m, n) == 0)
			continue;
		for (row = m, col = n;sum <= 5;row++, col++)
			if (a[row][col] != flag)
				break;
			else
				sum++;
		if (sum == 5)
		{
			printf("%cp%d success\n", flag, flag);
			exit(1);
		}
	}
}

void testUpToDown(int i, int j, int flag)
{
	int m, sum, row;
	for (m = i - 4;m <= i;m++)
	{
		sum = 0;
		if (isOut(m, j) == 0)
			continue;
		for (row = m;sum <= 5;row++)
			if (a[row][j] != flag)
				break;
			else
				sum++;
		if (sum == 5)
		{
			printf("%cp%d 恭喜获胜\n", flag, flag);
			exit(1);
		}
	}
}

void testLeft(int i, int j, int flag)
{
	int m, n, sum, row, col;
	for (m = i - 4, n = j + 4;m <= i;m++, n--)
	{
		sum = 0;
		if (isOut(m, n) == 0)
			continue;
		for (row = m, col = n;sum <= 5;row++, col--)
			if (a[row][col] != flag)
				break;
			else
				sum++;
		if (sum == 5)
		{
			printf("%cp%d 恭喜获胜\n", flag, flag);
			exit(1);
		}
	}
}

void testLeftToRight(int i, int j, int flag)
{
	int n, sum, col;
	for (n = j - 4;n <= j;n++)
	{
		sum = 0;
		if (isOut(i, n) == 0)
			continue;
		for (col = n;sum <= 5;col++)
			if (a[i][col] != flag)
				break;
			else
				sum++;
		if (sum == 5)
		{
			printf("%cp%d 恭喜获胜\n", flag, flag);
			exit(1);
		}
	}
}
void test(int i, int j, int flag)//对棋子时候5连测试
{
	if (a[i - 1][j - 1] == flag || a[i + 1][j + 1] == flag)//这是对右对角线的判断
		testRight(i, j, flag);
	if (a[i - 1][j] == flag || a[i + 1][j] == flag)//这是对上下的判断
		testUpToDown(i, j, flag);
	if (a[i - 1][j + 1] == flag || a[i + 1][j - 1] == flag)//这是对左对角线的判断
		testLeft(i, j, flag);
	if (a[i][j - 1] == flag || a[i][j + 1] == flag)//这是对左右判断
		testLeftToRight(i, j, flag);
}

int main()//本场比赛中，p1玩家用printf("%c ",1);p2玩家用printf("%c ",2);
{
	//初始化棋盘
	int total = 0, flag;//total作为计算是否平局以及打印时通过%2，存入a数组中1和2，和打印在显示器上不同棋子
	int i, j;
	memset(a, 0, sizeof(a) / sizeof(int));//string类方法填充a数组
	system("color 70");//控制台颜色
	print();//打印棋盘
	while (1)//开始下棋
	{
		if (total == 100)
		{
			printf("和棋\n");//平局
			break;
		}

		printf("%cp%d 请落子\n", total % 2 + 1, total % 2 + 1);
		flag = total % 2 + 1;
		scanf("%d%d", &i, &j);
		while (i < 0 || i>9 || j < 0 || j>9 || a[i][j] != 0)
		{
			printf("输入位置不对,重新输入\n");
			scanf("%d%d", &i, &j);
		}
		a[i][j] = flag;
		system("cls");
		printf("%cp%d pre put at [%d] [%d]\n", flag, flag, i, j);//打印之前一个人下的位置
		print();
		test(i, j, flag);
		total++;
	}
	return 0;
}
	